<?php

class CategoryController extends RController
{

    public function init()
    {
        parent::init();
        $baseUrl = Yii::app()->baseUrl; 
        $cs = Yii::app()->getClientScript();
        $cs->registerScriptFile($baseUrl.'/js/bootstrap-fileupload.min.js'); 
        $cs->registerCssFile($baseUrl.'/css/bootstrap-fileupload.min.css'); 
    }
    
    public function actionIndex()
    {
        
    }

    public function actionList()
    {
        if (!empty($_POST['SearchForm'])) {
            $searchFormData = $_POST['SearchForm'];
            $searchForm = new SearchForm();
            $searchForm->setAttributes($searchFormData);
            $data = $searchForm->getAttributes();
            $keyword = $data['keyword'];

            $criteria = new CDbCriteria();
            if (!empty($keyword)) {
                $criteria->condition = "MATCH (title) AGAINST ('$keyword' IN BOOLEAN MODE)";
            }

            $dataProvider = new CActiveDataProvider('Category', array(
                        'criteria' => $criteria
                    ));
        }

        if (empty($_POST['SearchForm'])) {
            $dataProvider = Category::model()->listCategory();
        }

        $this->render('list', array(
            'dataProvider' => $dataProvider
        ));
    }

    public function actionCreate()
    {

        $model = new Category();
        var_dump($_POST);
        if (isset($_POST['Category'])) {
            $model->setAttributes($_POST['Category']);
            $model->icon = CUploadedFile::getInstance($model, 'icon');
            if ($model->validate() && $model->save()) {
                if($model->icon!=null){
                    $model->icon->saveAs(Yii::app()->basePath . '/../img/upload/' . $model->icon);
                }
                Yii::app()->user->setFlash('success', 'Bạn đã thêm danh mục mới thành công');
            } else {
                Yii::app()->user->setFlash('error', 'Có lỗi xảy ra');
            }
        }

        $this->breadcrumbs = array(
            'Danh mục' => array('/product/category/list'),
            'Tạo danh mục mới'
        );

        $this->render('_form', array(
            'model' => $model,
        ));
    }

    public function actionUpdate()
    {
        $id = Yii::app()->getRequest()->getQuery('id');
        $model = Category::model()->findByPk($id);
        
        //var_dump($_POST['Category']);die();
        if (isset($_POST['Category'])) {
            $model->setAttributes($_POST['Category']);
            $icon = CUploadedFile::getInstance($model, 'icon');
            
            if($icon!=null){
                $model->icon = $icon;
            }
            
            if ($model->validate() && $model->save()) {
                 if($icon!=null){
                    $model->icon->saveAs(Yii::app()->basePath . '/../img/upload/' . $model->icon);
                }
                Yii::app()->user->setFlash('success', 'Bạn đã cập nhật danh mục thành công');
            } else {
                Yii::app()->user->setFlash('error', 'Có lỗi xảy ra');
            }
        }

        $this->breadcrumbs = array(
            'Danh mục' => array('/product/category/list'),
            'Tạo danh mục mới'
        );

        $this->render('_form', array(
            'model' => $model,
        ));
    }

    public function actionDelete()
    {
        
        $id = Yii::app()->getRequest()->getQuery('id');
        $model = Category::model()->findByPk($id);
        $model->delete();
    }
    
     public function filters()
    {
        return array(
            'rights'
        );
    }

}